package com.software.library_conquer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

/**
 * Created by CwT on 15/6/2.
 */
public class MyActivity extends Activity implements View.OnClickListener
{
    protected SlidingMenu side_drawer;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        initSlidingMenu();
        LinearLayout text1 = (LinearLayout)findViewById(R.id.floor1);
        LinearLayout text2 = (LinearLayout)findViewById(R.id.floor2);
        LinearLayout text3 = (LinearLayout)findViewById(R.id.floor3);
        LinearLayout text4 = (LinearLayout)findViewById(R.id.floor4);
        text1.setOnClickListener(this);
        text2.setOnClickListener(this);
        text3.setOnClickListener(this);
        text4.setOnClickListener(this);
    }

    protected void initSlidingMenu() {
        side_drawer = new DrawerView(this).initSlidingMenu();
    }

    private long mExitTime;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if(side_drawer.isMenuShowing()){// ||side_drawer.isSecondaryMenuShowing()){
                side_drawer.showContent();
            }else {
                if ((System.currentTimeMillis() - mExitTime) > 2000) {
                    Toast.makeText(this, "再按一次退出",
                            Toast.LENGTH_SHORT).show();
                    mExitTime = System.currentTimeMillis();
                } else {
                    finish();
                }
            }
            return true;
        }
        //拦截MENU按钮点击事件，让他无任何操作
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            if(!side_drawer.isMenuShowing())// || !side_drawer.isSecondaryMenuShowing())
                side_drawer.showMenu();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void toggleMenu(View view)
    {
        if (side_drawer.isMenuShowing())
            side_drawer.showContent();
        else
            side_drawer.showMenu();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        int arg = 1;
        switch(id){
            case R.id.floor1:
                break;
            case R.id.floor2:
                arg = 2;
                break;
            case R.id.floor3:
                arg = 3;
                break;
            case R.id.floor4:
                arg = 4;
                break;
        }
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("floor", arg);
        startActivity(intent);
        finish();
    }
}
